
/**
 * Copyright 2014-2020  the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.upb.webserver.account.manager;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.account.PasswordInfo;
import com.upb.webserver.common.pojo.account.manager.ReqAddManagerVO;
import com.upb.webserver.common.pojo.account.manager.ReqModifyManagerVO;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import com.upb.webserver.dao.entity.TbManager;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Log4j2
@RestController
@RequestMapping(value = "manager")
public class ManagerController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private ManagerService managerService;

    /**
     * register developer.
     *
     * @param param
     * @param result
     * @return
     */
    @PostMapping
    public BaseResponse addNewManager(@RequestBody @Valid ReqAddManagerVO param,
                                      BindingResult result,
                                      HttpServletRequest request) {
        log.info("start exec method [addNewManager]. param:{}", JsonUtils.objToString(param));
        AccountCommon accountCommon = accountService.getAccountCommon(request);

        TbManager tbmanager = managerService.addManager(accountCommon.getRoleName(), param);
        BaseResponse response = BaseResponse.initSuccess();
        response.setData(tbmanager);
        log.info("success exec method [addNewManager].response:{}",JsonUtils.objToString(response));
        return response;
    }


    /**
     * @param pageNumber
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping(value = "/list")
    public BasePageResponse queryManagerByPage(@RequestParam("pageNumber") Integer pageNumber,
                                               @RequestParam("pageSize") Integer pageSize,
                                               @RequestParam(value = "status", required = false) Byte status,
                                               HttpServletRequest request) {
        log.info("start exec method [queryManagerByPage]. pageNumber:{} pageSize:{} status:{}", pageNumber, pageSize, status);
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        BasePageResponse response = managerService.queryManagerByPage(accountCommon.getId(), accountCommon.getRoleName(), status, pageNumber, pageSize);
        log.info("success exec method [queryManagerByPage]. result:{}", JsonUtils.objToString(response));
        return response;
    }


    /**
     * @param param
     * @param result
     * @return
     */
    @PatchMapping
    public BaseResponse modifyManagerAccount(@RequestBody @Valid ReqModifyManagerVO param, BindingResult result) {
        log.info("start exec method [modifyManagerAccount]. param:{}", JsonUtils.objToString(param));
        //get AccountCommon
        managerService.modifyManagerAccount(param);
        log.info("success exec method [modifyManagerAccount].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }


    /**
     * @param param
     * @param request
     * @param result
     * @return
     */
    @PatchMapping("/changePassword")
    public BaseResponse updatePassword(@RequestBody @Valid PasswordInfo param, HttpServletRequest request, BindingResult result) {
        log.info("start exec method [updatePassword]. param:{}", JsonUtils.objToString(param));
        //get AccountCommon
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        managerService.updatePassword(accountCommon.getId(), param.getOldAccountPwd(), param.getNewAccountPwd());
        log.info("success exec method [register].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }


    /**
     * @param managerId
     * @return
     */
    @DeleteMapping(value = "{managerId}")
    public BaseResponse deleteByManagerId(@PathVariable("managerId") Integer managerId) {
        log.info("start exec method [deleteByManagerId]. managerId:{}", managerId);
        managerService.deleteByManagerId(managerId);
        log.info("success exec method [deleteByManagerId].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }

}
